# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        You are given two non-empty linked lists representing two non-negative integers. 
        The digits are stored in reverse order, and each of their nodes contains a single digit. 
        Add the two numbers and return the sum as a linked list.
        You may assume the two numbers do not contain any leading zero, except the number 0 itself.

        给你两个非空的链表，表示两个非负的整数。它们每位数字都是按照逆序的方式存储的，并且每个节点只能存储一位数字。
        请你将两个数相加，并以相同形式返回一个表示和的链表。
        你可以假设除了数字 0 之外，这两个数都不会以 0开头。
        
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        result = ListNode()
        p = result
        AF = 0
        while l1!=None or l2!=None:
            # 同时遍历两个链表
            # AF为进位标志
            # 将两个链表全部遍历完
            # 使用python的三目运算表达式进行判断赋值
            val1 = 0 if l1==None else l1.val
            val2 = 0 if l2==None else l2.val
            temp_value = val1 + val2 + AF
            AF = 1 if temp_value > 9 else 0
            temp_LNode = ListNode(temp_value % 10)
            if l1:
                l1 = l1.next
            if l2: 
                l2 = l2.next
            p.next = temp_LNode
            p = p.next
        if AF == 1:
            p.next = ListNode(1)
        result = result.next
        return result